package com.photography.wedding.controller;

import com.photography.wedding.pojo.News;
import com.photography.wedding.pojo.Picture;
import com.photography.wedding.service.NewsService;
import com.photography.wedding.service.PhotoService;
import com.photography.wedding.util.ReturnUtil;
import com.photography.wedding.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class NewsJsonController {
    @Autowired
    private NewsService newsService;

    @ResponseBody
    @RequestMapping(value = "/member/addNews",method = RequestMethod.POST)
    public ModelMap addNews(@Validated @RequestBody News news, BindingResult result){
        try {

            if (news.getNewsKeywords() != null){
                news.setNewsKeywords(news.getNewsKeywords());
            }

            news.setId(UuidUtil.getUUID());

            int count = newsService.insertSelective(news);
            if (count == 0){
                return ReturnUtil.error("添加新闻失败");
            }

            return ReturnUtil.success("添加新闻成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }

    }


    @ResponseBody
    @RequestMapping(value = "/member/selectStripNews",method = RequestMethod.POST)
    public ModelMap selectStripNews(@Validated @RequestBody News news, BindingResult result){
        try {

            if(StringUtils.isEmpty(news.getId())){
                return ReturnUtil.error("新闻ID不能为空", null, null);
            }

            News news1 = newsService.selectByPrimaryKey(news.getId());

            if (news1 == null){
                return ReturnUtil.error("暂无数据信息");
            }

            return ReturnUtil.success("查询图片成功", news1);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }

    }

    @ResponseBody
    @RequestMapping(value = "/member/deletNwes",method = RequestMethod.POST)
    public ModelMap deletNwes(@Validated @RequestBody News news, BindingResult result){
        try {
            if(StringUtils.isEmpty(news.getId())){
                return ReturnUtil.error("新闻ID不能为空", null, null);
            }

            int count = newsService.deleteByPrimaryKey(news.getId());
            if (count == 0){
                return ReturnUtil.error("删除新闻失败");
            }

            return ReturnUtil.success("删除新闻成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }

    }


    @ResponseBody
    @RequestMapping(value = "/member/updateNews",method = RequestMethod.POST)
    public ModelMap updateNews(@Validated @RequestBody News news, BindingResult result){
        try {
            if(StringUtils.isEmpty(news.getId())){
                return ReturnUtil.error("新闻ID不能为空");
            }

            if (news.getNewsKeywords() != null){
                news.setNewsKeywords(news.getNewsKeywords());
            }

            int count = newsService.updateByPrimaryKeySelective(news);
            if (count == 0){
                return ReturnUtil.error("更新新闻失败");
            }

            return ReturnUtil.success("更新新闻成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", "发生未知错误", null);
        }

    }





}
